<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML>
<html><head>
<title>diary.ru</title>

<link rel="stylesheet" href="background.css" media="all" type="text/css"/>
<script type="text/javascript" src="utils.js"></script>
<script type="text/javascript" src="rescache.js"></script>
<script type="text/javascript" src="fetcher.js"></script>

<script type="text/javascript">
	function dbg(msg) {
		opera.postError(msg);
	}
	
	/* Shows/hides an html element */
	function showControl(id, state) {
		var ctl = document.getElementById(id);
		if (state)
			ctl.style.display=""
		else
			ctl.style.display="none";
	}
	
	
	/* Change sleepy look message */
	function setSleepyMessage(msg) {
		message=document.getElementById("_dru_message");
		message.innerHTML=msg;
	}	
	
	/* Activates sleepy look */
	function sleepyLook(msg) {
		/* We might not yet have a dru, even */
		var dru = window.dru;
		if (dru && dru.baseCss) {
			document.head.removeChild(dru.baseCss);
			dru.baseCss=null;
		};
		if (dru && dru.journalCss) {
			document.head.removeChild(dru.journalCss);
			dru.journalCss=null;
		};
		if (dru && dru.parentCss) {
			document.head.removeChild(dru.parentCss);
			dru.parentCss=null;
		};
		
		setSleepyMessage(msg);
		
		if (opera.contexts.speeddial) {
			opera.contexts.speeddial.title="@дневники";
			
			var sd_link="", default_link="http://www.diary.ru";
			switch (parseInt(widget.preferences['link-type'], 10)) {
				case 0: 
					/* Будет задано, если дизайн задан жёстко */
					if (dru.d_design_link)
						sd_link=dru.d_design_link
					else
						sd_link=default_link;
					break;
				case 1: sd_link=default_link; break; /* my_link в sleepy всегда недоступно */
				case 2: sd_link="http://www.diary.ru"; break;
				case 3: sd_link=widget.preferences['link-to'];
				default: sd_link=default_link; break;
			}
			opera.contexts.speeddial.url=sd_link;
		}
		
		/* Switch */
		sleepy=document.getElementById("_dru_sleepy");
		alive=document.getElementById("_dru_alive");
		sleepy.style.display="";
		alive.style.display="none";
	}
	
	/* Activates alive look, takes data from window.dru */
	function aliveLook() {
		var dru=window.dru;
		
		/* Fetch resources and store it locally */
		/* Not release-ready.
		var b_photo_thumb=rc_fetchBase64(dru.d_photo_thumb);
		var b_css=rc_fetch(dru.d_css);
		*/
		
		d_photo=document.getElementById("_dru_d_photo");
		if ((dru.d_photo_thumb) || (dru.d_photo)) {
			d_photo.style.display="";
			if (dru.d_photo_thumb)
				d_photo.src=dru.d_photo_thumb;
			else
				d_photo.src=dru.d_photo;
		} else {
			d_photo.style.display="none";
			d_photo.src="";
		}
		/* Use local resources (for offline view):
		d_photo.src="data:image/jpeg;base64,"+b_photo_thumb;
		*/
		
		var sd_author="";
		switch (parseInt(widget.preferences['heading-format'], 10)) {
			case 0: sd_author=dru.d_author; break;
			case 1:
				if (dru.d_title)
					sd_author=dru.d_title;
				else
					sd_author="Нет дневника";
				break;
			default: sd_author=dru.d_author; break;
		}
		d_author=document.getElementById("_dru_d_author");
		d_author.innerHTML=sd_author;
		
		/* Пытаемся сохранить подобие структуры тегов из дайри,
			чтобы применённые теги максимально подошли. */
		d_replies=document.getElementById("_dru_d_replies");
		if (dru.d_replies>0) {
			d_replies.innerHTML=" &ndash; <a href=#>"+dru.d_replies+"</a>"
			showControl("myDiaryLink", true);
		} else {
			d_replies.innerHTML="";
			showControl("myDiaryLink", false);
		}
		
		d_discussions=document.getElementById("_dru_d_discussions");
		if (dru.d_discussions>0) {
			d_discussions.innerHTML=" &ndash; <a href=#>"+dru.d_discussions+"</a>"
			showControl("discussionLink", true);
		} else {
			d_discussions.innerHTML="";
			showControl("discussionLink", false);
		}
		
		d_umails=document.getElementById("_dru_d_umails");
		if (dru.d_umails>0) {
			d_umails.innerHTML=" &ndash; <a href=# id=menuNewUmails>"+dru.d_umails+"</a>"
			showControl("umailLink", true);
		} else {
			d_umails.innerHTML="";
			showControl("umailLink", false);
		}
		
		/* Если вообще ничего нет, показываем сообщение "" */
		if ((dru.d_replies==0) && (dru.d_discussions==0) && (dru.d_umails==0))
			showControl("_dru_no_new", true)
		else
			showControl("_dru_no_new", false);
		
		if (opera.contexts.speeddial) {
			var sd_title="";
			switch (parseInt(widget.preferences['title-format'], 10)) {
				case 0:
					if (dru.d_title)
						sd_title=dru.d_title;
					else
						sd_title="Нет дневника";
					break;
				case 1:
					if (dru.d_title)
						sd_title=dru.d_title;
					else
						sd_title="Нет дневника";
					sd_title="@дневники — "+sd_title;
					break;
				case 2: sd_title=dru.d_author; break;
				case 3: sd_title="@дневники — "+dru.d_author; break;
				default: sd_title="@дневники"; break;
			}
			opera.contexts.speeddial.title=sd_title;
			
			var
				sd_link="",
				default_link="http://www.diary.ru";
			switch (parseInt(widget.preferences['link-type'], 10)) {
				case 0:
					if (dru.d_design_link)
						sd_link=dru.d_design_link;
					else
						sd_link=default_link;
					break;
				case 1:
					if (dru.d_my_link)
						sd_link=dru.d_my_link;
					else
						sd_link=default_link;
					break;
				case 2: sd_link=default_link; break;
				case 3: sd_link=widget.preferences['link-to']; break;
				case 4: sd_link=dru.d_profile_link; break;
				case 5: sd_link=dru.d_favlink; break;
				case 6: sd_link=dru.d_discussionlink; break;
				case 7: sd_link=dru.d_umaillink; break;
				default: sd_link=default_link; break;
			}
			opera.contexts.speeddial.url=sd_link;
		}
		
		/* Последним применяем CSS, т.к. оно заменит фон страницы,
		и не стоит этого делать сильно раньше переключения sleepy/alive */
		if (!dru.baseCss) {
			var st = document.createElement("link");
			st.rel="stylesheet";
			st.type="text/css";
			st.href="http://static.diary.ru/style/journal.css";
			st.media="all";
			document.head.appendChild(st);
			dru.baseCss=st;
		}
		/* Reload user css every time */
		if (dru.journalCss) {
			document.head.removeChild(dru.journalCss);
			dru.journalCss=null;
		};
		if (dru.parentCss) {
			document.head.removeChild(dru.parentCss);
			dru.parentCss=null;
		};
		/* Apply user css */
		if (!dru.journalCss) {
			var d_css=dru.d_journal_css;
			if (!d_css) d_css="/default-design.css";
			var st = document.createElement("link");
			st.rel="stylesheet";
			st.type="text/css";
			st.href=d_css;
			st.media="all";
			document.head.appendChild(st);
			dru.journalCss=st;
		}
		if (!dru.parentCss && dru.d_parent_css) {
			var st = document.createElement("link");
			st.rel="stylesheet";
			st.type="text/css";
			st.href=dru.d_parent_css;
			st.media="all";
			document.head.appendChild(st);
			dru.d_parent_css=st;
		}
		
		/* Switch */
		sleepy=document.getElementById("_dru_sleepy");
		alive=document.getElementById("_dru_alive");
		sleepy.style.display="none";
		alive.style.display="";
	}
	
	function updateDru() {
		var dru=window.dru;
		if (!dru) return;
		
		if (dru.design_source != widget.preferences['design-source']) {
			/* Дизайн требует перезагрузки -- покажем заставку */
			sleepyLook("Подождите...");
			dru.design_source=widget.preferences['design-source'];
		} else
			/* Всё равно пишем Loading..., но только если sleepy и так открыт */
			setSleepyMessage("Подождите...");

		if (!dru.update()) {
			/* Restore standard look */
			var msg="";
			switch (dru.error) {
			case 1: msg="Нет сети."; break;
			case 2: msg="Залогиньтесь."; break;
			case 3: msg="Дизайн не найден."; break;
			default: msg="Всё сломалось."; break;
			}
			sleepyLook(msg);
			return;
		};
		
		/* Apply special look */
		aliveLook();
	}
	
	function preferencesChanged() {
		if (typeof widget != "undefined")
			var interval = (widget.preferences['interval'] || 0) * 1000;
		else
			var interval = 1000; //debug case

		/* Reset timer: interval could have been changed */
		if(window.updateTimer)
			clearInterval(window.updateTimer);
		if (interval)
			window.updateTimer = setInterval("updateDru()", interval);
		
		window.dru.abort();
	}
	
	window.addEventListener("load", function() {
		window.dru = new DiaryRuFetcher();
		sleepyLook("Подождите...");
		
		preferencesChanged();
		updateDru();
	}, false);
</script>

</head>
<body class="block_design _dru_body_override">
<div id="_dru_sleepy" style="" class="_dru_centerwrap"><div class="_dru_center">
	<img id="_dru_logo" src="images/diary_logo_70.png" />
	<div id="_dru_message"></div>
</div></div>
<div id="_dru_alive" style="display:none;">
	<div class="_dru_centerwrap">
	<div class="_dru_center">
	<div class="_dru_collapse">
		<!-- Пытаемся сохранить подобие структуры тегов из дайри,
			чтобы применённые стили максимально подошли. -->
		<div id="wrapper" class="_dru_wrapper_override">
		<div id="side" class="_dru_side_override">
		<div id="bordered" class="bordered _dru_bordered_override">
		<div id="myDiaryLinks" class="menuSection _dru_myDiaryLinks_override">
			<div id="_dru_tbl_results">
			<div id="_dru_td_vcard">
				<div id="thisDiaryPhoto" class="_dru_thisDiaryPhoto_override"><img id="_dru_d_photo" /></div>
			</div>
			<div id="_dru_td_stats">
				<!-- Пытаемся сохранить подобие структуры тегов из дайри,
					чтобы применённые стили максимально подошли. -->
				<ul>
					<li id="_dru_authorName"><a id="authorName" class="_dru_authorName_override"><span id="_dru_d_author"></span></a></li>
					<li id="myDiaryLink"><span><a href=#><b>Мой дневник</b></a><span class="count_box" id="_dru_d_replies"></span></span></li>
					<li id="umailLink"><a href=#><span>U-mail</span></a><span class="count_box" id="_dru_d_umails"></span></li>
					<li id="discussionLink"><a href=#><span>Дискуссии</span></a><span class="count_box" id="_dru_d_discussions"></span></li>
					<li id="_dru_no_new"><a href=#><span>Новых сообщений нет.</span></a></li>
				</ul>
			</div>
			</div>
		</div>
		</div>
		</div>
		</div>
	</div></div></div>
</div>
</body>
</html>